package net.wantongtech.xa.uploadbasicinfo.dao;

import com.wtkj.dao.impl.BaseDataTemplate;
import com.wtkj.vo.dzzf.base.xxjh.MessageSend;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 统计错误的合并交易服务DAO
 *
 * @author MaShibo
 * 日期 :  2020/2/6
 */
@Repository
public class StatsErrorIntegratedTransOutDataDao extends BaseDataTemplate {

    public List<MessageSend> queryErrorMessageSend(List<String> integratedIdList) {
        final String baseSql =
                "SELECT s.listid,\n" +
                        "       s.msgid,\n" +
                        "       s.channeltype,\n" +
                        "       s.createon,\n" +
                        "       s.send,\n" +
                        "       s.signsource,\n" +
                        "       s.responseinfo,\n" +
                        "       s.pushnum\n" +
                        "  FROM etc_messagesend s\n" +
                        " WHERE EXISTS (SELECT 1\n" +
                        "          FROM tr_integrated_transout tr\n" +
                        "         WHERE tr.messageid = s.msgid\n" +
                        "           AND tr.channeltype = s.channeltype\n" +
                        "           AND tr.integratedid IN (%s))";
        List<String> placeHolderValList = integratedIdList
                .stream()
                .map(integratedId -> String.format("'%s'", integratedId))
                .collect(Collectors.toList());
        String sql = String.format(baseSql, String.join(",", placeHolderValList));

        return jdbcTemplate.query(sql,
                BeanPropertyRowMapper.newInstance(MessageSend.class));
    }


    public List<String> queryNoSentIntegratedId(String noneSendIdJoining) {
        final String baseSql =
                "SELECT tr.integratedid\n" +
                        "  FROM tr_integrated_transout tr\n" +
                        "  JOIN etc_messagesend s\n" +
                        "    ON tr.messageid = s.msgid\n" +
                        "   AND tr.channeltype = s.channeltype\n" +
                        " WHERE s.listid IN (%s)";
        return jdbcTemplate.queryForList(String.format(baseSql, noneSendIdJoining), String.class);
    }

    public int resetIntegratedTransOutSendInfo(String integratedId) {
        final String sql =
                "UPDATE tr_integrated_transout a\n" +
                        "   SET a.state = 0, a.sendlistid = NULL, a.messageid = NULL\n" +
                        " WHERE a.integratedid = ?";
        return jdbcTemplate.update(sql, integratedId);
    }

    public int deleteIntegratedTransOutById(String integratedId) {
        final String sql = " DELETE FROM tr_integrated_transout tr WHERE tr.integratedid = ?";
        return jdbcTemplate.update(sql, integratedId);
    }

    public int resetPortalTransOutIntegratedInfo(String integratedId) {
        final String sql =
                " UPDATE tr_portal_transout tr\n" +
                        "    SET tr.integratedid = NULL, tr.isintegrated = 0\n" +
                        "  WHERE tr.integratedid = ?";
        return jdbcTemplate.update(sql, integratedId);
    }

    public int deleteMessageSendById(String listId) {
        final String sql = "DELETE FROM etc_messagesend a WHERE a.listid = ?";
        return jdbcTemplate.update(sql, listId);
    }
}
